﻿using System;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YRevitJig;

namespace HYElectricDim
{
	public class ElectricDimJig : XUiJigBase
	{
		public XYZ StartPoint { get; set; }

		public XYZ EndPoint { get; set; }

		public XUiJigBase.EResetPoint ResetPoint { get; set; }

		public ElectricDimJig.EDimType DimType { get; set; }

		public double RectWidth { get; set; }

		public double RectHeight { get; set; }

		public ElectricDimJig(UIDocument uiDoc) : base(uiDoc)
		{
			this.ResetPoint = XUiJigBase.EResetPoint.eNoOper;
		}

		public XYZ PickDimPoint(XYZ ptStart, ElectricDimJig.EDimType dimType, string strPrompt = "请确定标注点：")
		{
			try
			{
				this.DimType = dimType;
				this.StartPoint = ptStart;
				this.EndPoint = base.UiDoc.Selection.PickPoint(strPrompt);
				this.EndPoint = this.GetResetPoint(this.StartPoint, this.EndPoint);
			}
			catch
			{
				return null;
			}
			return this.EndPoint;
		}

		public XYZ GetResetPoint(XYZ ptBase, XYZ ptUser)
		{
			XYZ result = ptUser;
			if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				result = JigFuncs.GetOrthoPoint(base.UiDoc, ptUser, ptBase);
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			if (this.m_ptCur.IsEmpty)
			{
				return;
			}
			XYZ xyz = base.Client2Revit(this.m_ptCur);
			if (this.ResetPoint == XUiJigBase.EResetPoint.eByOrtho)
			{
				xyz = JigFuncs.GetOrthoPoint(base.UiDoc, xyz, this.StartPoint);
			}
			XGraphics xgraphics = new XGraphics(paintEventArgs.Graphics, base.JigPen);
			new XLine2D(base.Revit2Client(this.StartPoint), base.Revit2Client(xyz)).Draw(xgraphics);
			if (this.DimType == ElectricDimJig.EDimType.eSideBorder)
			{
				this.DrawRectBySideBorder(this.StartPoint, xyz, xgraphics);
				return;
			}
			if (this.DimType == ElectricDimJig.EDimType.eBottomCorner)
			{
				this.DrawRectByBottomCorner(this.StartPoint, xyz, xgraphics);
				return;
			}
			if (this.DimType == ElectricDimJig.EDimType.eFourCorner)
			{
				this.DrawRectByFourCorner(this.StartPoint, xyz, xgraphics);
			}
		}

		private void DrawRectBySideBorder(XYZ ptBase, XYZ ptJig, XGraphics graphics)
		{
			XYZ xyz = XYZ.BasisX;
			if (ptJig.X < ptBase.X)
			{
				xyz = -XYZ.BasisX;
			}
			XYZ xyz2 = ptJig + xyz * 0.1 * this.RectWidth;
			new XLine2D(base.Revit2Client(ptJig), base.Revit2Client(xyz2)).Draw(graphics);
			XYZ xyz3 = xyz2 + xyz * this.RectWidth;
			XYZ point = xyz2 + XYZ.BasisY * 0.5 * this.RectHeight;
			XYZ point2 = xyz3 + XYZ.BasisY * 0.5 * this.RectHeight;
			new XLine2D(base.Revit2Client(point), base.Revit2Client(point2)).Draw(graphics);
			XYZ point3 = xyz2 - XYZ.BasisY * 0.5 * this.RectHeight;
			XYZ point4 = xyz3 - XYZ.BasisY * 0.5 * this.RectHeight;
			new XLine2D(base.Revit2Client(point3), base.Revit2Client(point4)).Draw(graphics);
			new XLine2D(base.Revit2Client(point), base.Revit2Client(point3)).Draw(graphics);
			new XLine2D(base.Revit2Client(point2), base.Revit2Client(point4)).Draw(graphics);
		}

		private void DrawRectByBottomCorner(XYZ ptBase, XYZ ptJig, XGraphics graphics)
		{
			XYZ xyz = XYZ.BasisX;
			if (ptJig.X < ptBase.X)
			{
				xyz = -XYZ.BasisX;
			}
			XYZ xyz2 = ptJig + xyz * this.RectWidth;
			new XLine2D(base.Revit2Client(ptJig), base.Revit2Client(xyz2)).Draw(graphics);
			XYZ point = ptJig + XYZ.BasisY * this.RectHeight;
			XYZ point2 = xyz2 + XYZ.BasisY * this.RectHeight;
			new XLine2D(base.Revit2Client(point), base.Revit2Client(point2)).Draw(graphics);
			new XLine2D(base.Revit2Client(point), base.Revit2Client(ptJig)).Draw(graphics);
			new XLine2D(base.Revit2Client(point2), base.Revit2Client(xyz2)).Draw(graphics);
		}

		private void DrawRectByFourCorner(XYZ ptBase, XYZ ptJig, XGraphics graphics)
		{
			XYZ xyz = XYZ.BasisX;
			if (ptJig.X < ptBase.X)
			{
				xyz = -XYZ.BasisX;
			}
			XYZ xyz2 = XYZ.BasisY;
			if (ptJig.Y < ptBase.Y)
			{
				xyz2 = -XYZ.BasisY;
			}
			XYZ xyz3 = ptJig + xyz * this.RectWidth;
			new XLine2D(base.Revit2Client(ptJig), base.Revit2Client(xyz3)).Draw(graphics);
			XYZ point = ptJig + xyz2 * this.RectHeight;
			XYZ point2 = xyz3 + xyz2 * this.RectHeight;
			new XLine2D(base.Revit2Client(point), base.Revit2Client(point2)).Draw(graphics);
			new XLine2D(base.Revit2Client(point), base.Revit2Client(ptJig)).Draw(graphics);
			new XLine2D(base.Revit2Client(point2), base.Revit2Client(xyz3)).Draw(graphics);
		}

		public enum EDimType
		{
			eSideBorder,
			eBottomCorner,
			eFourCorner
		}
	}
}
